﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BenefitHelper.Data.Week
{
    /// <summary>
    /// 账户周预期值
    /// </summary>
    public class AccountWeekExpected
    {
        public int Id { get; set; }

        public int AccountId { get; set; }

        public virtual BenefitHelper.Sys.Account Account { get; set; }
        /// <summary>
        /// 当前周
        /// </summary>
        public int Week { get; set; }
        /// <summary>
        /// 年份
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 月份
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        public void Delete(int week, int month, int year, DB.DBManager db)
        {
            var query = from t in db.AccountWeekExpected where t.Week == week && t.Year == year select t;
            foreach (Data.Week.AccountWeekExpected c in query)
            {
                db.AccountWeekExpected.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 初始化账户周预期值
        /// </summary>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void InitAccountWeekExpected(int week, int month, int year, DB.DBManager db)
        {
            var query = db.Accounts.ToList();
            foreach (Sys.Account account in query)
            {
                Data.Week.AccountWeekExpected change = new Data.Week.AccountWeekExpected();
                change.AccountId = account.Id;

                var existQuery = from t in db.AccountWeekExpected where t.AccountId == account.Id && t.Week == week && t.Year == year select t;
                if (existQuery.Count() > 0)
                {
                  //  change = existQuery.First();
                    var _query2 = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year orderby t.Id descending select t;
                    double avgbenefit = 0;
                    if (_query2.Count() > 0)
                    {
                        avgbenefit = Math.Round(_query2.Average(a => a.DayCount), 2);
                    }
                    existQuery.First().DayCount = avgbenefit * 5;
                }
                else
                {
                    change.Week = week;
                    change.Month = month;
                    change.Year = year;
                    var _query2 = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistory.WPdate == week && t.TradeHistory.YPdate == year orderby t.Id descending select t;
                    double avgbenefit = 0;
                    if (_query2.Count() > 0)
                    {
                        avgbenefit = Math.Round(_query2.Average(a => a.DayCount), 2);
                    }
                    change.DayCount = avgbenefit * 5;
                    db.AccountWeekExpected.Add(change);
                }
            }
            db.SaveChanges();
        }

    }
}